{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 均值移除：调整数据分布，以列为单位，每列的均值为0，标准差为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import sklearn.preprocessing as sp #数据预处理模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = np.array([[3.0,-1.0,2.0],\n",
    "                   [0.0,4.0,3.0],\n",
    "                   [1.0,-4.0,2.0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每列均值: [ 5.55111512e-17  0.00000000e+00 -2.96059473e-16]\n",
      "每列标准差: [1. 1. 1.]\n"
     ]
    }
   ],
   "source": [
    "std_sample = sample.copy() #自己手推\n",
    "\n",
    "for col in std_sample.T:\n",
    "    col_mean = col.mean() #每列的均值\n",
    "    col_std = col.std() #每列的标准差\n",
    "    \n",
    "    col -= col_mean #每个元素减去均值\n",
    "    col /= col_std #每个元素除以标准差\n",
    "    \n",
    "print('每列均值:',std_sample.mean(axis=0))\n",
    "print('每列标准差:',std_sample.std(axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.33630621 -0.20203051 -0.70710678]\n",
      " [-1.06904497  1.31319831  1.41421356]\n",
      " [-0.26726124 -1.1111678  -0.70710678]]\n"
     ]
    }
   ],
   "source": [
    "# 使用sklearn中提供的借口实现标准化\n",
    "res = sp.scale(sample)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.33630621 -0.20203051 -0.70710678]\n",
      " [-1.06904497  1.31319831  1.41421356]\n",
      " [-0.26726124 -1.1111678  -0.70710678]]\n"
     ]
    }
   ],
   "source": [
    "print(std_sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
